package com.blue.waffle.iwc.game;

import java.sql.SQLException;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.blue.waffle.iwc.database.SqliteActionResolver;
import com.blue.waffle.iwc.database.SqliteDatabaseHelper;
import com.blue.waffle.iwc.screens.LoadingScreen;
import com.blue.waffle.iwc.screens.SplashScreen;
import com.blue.waffle.iwc.utils.GameManager;
import com.blue.waffle.iwc.utils.MusicManager;
import com.blue.waffle.iwc.utils.ParticleEffectLoader;
import com.blue.waffle.iwc.utils.PreferencesManager;
import com.blue.waffle.iwc.utils.SoundManager;

public class IWCGame extends Game {

	private SqliteActionResolver sqliteActionResolver;
	private MusicManager musicManager;
	private SoundManager soundManager;
	private PreferencesManager preferencesManager;
	private FPSLogger fpsLogger;
	private AssetManager assetManager;
	private static String APP_FILE_DIR;
	private SqliteDatabaseHelper databaseHelper;
	private OrthographicCamera camera;
	private GameManager gameManager;
	
	private static final boolean DEV_MODE=false;
	
	public final static int VIRTUAL_WIDTH = 800;
	public final static int VIRTUAL_HEIGHT = 480;

	public IWCGame(SqliteActionResolver sqliteActionResolver) {
		this.sqliteActionResolver = sqliteActionResolver;
		this.databaseHelper=new SqliteDatabaseHelper(sqliteActionResolver);
	}

	public SplashScreen getSplashScreen() {
		return new SplashScreen(this);
	}
	
	public static String getAPP_FILE_DIR() {
		if (APP_FILE_DIR==null) {
			APP_FILE_DIR=Gdx.files.getExternalStoragePath()+"/"+"Incredible_Word_Challenge";
		}
		return APP_FILE_DIR;
	}

	@Override
	public void create() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		this.camera=(new OrthographicCamera(1, h/w));
		this.assetManager=new AssetManager(new InternalFileHandleResolver());
		assetManager.setLoader(ParticleEffect.class, new ParticleEffectLoader(new InternalFileHandleResolver()));
		this.musicManager = new MusicManager(assetManager);
		this.soundManager = new SoundManager(assetManager);
		this.preferencesManager = new PreferencesManager();
		musicManager.setVolume(preferencesManager.getMusicVolume());
		musicManager.setActive(preferencesManager.isMusicActive());
		soundManager.setVolume(preferencesManager.getSoundVolume());
		soundManager.setActive(preferencesManager.isSoundActive());
		
		int db_version=preferencesManager.getDbVersion();
		
		if (db_version<=0 || db_version!=SqliteActionResolver.DB_VERSION) {
			try {
				sqliteActionResolver.init();
				preferencesManager.setDbVersion(SqliteActionResolver.DB_VERSION);
			} catch (SQLException e) {
				e.printStackTrace();
				System.exit(0);
			}
		}
		
		gameManager=new GameManager(this);
		fpsLogger=new FPSLogger();
		setScreen(new LoadingScreen(this));
	}
	
	public SqliteActionResolver getSqliteActionResolver() {
		return sqliteActionResolver;
	}

	public MusicManager getMusicManager() {
		return musicManager;
	}

	public SoundManager getSoundManager() {
		return soundManager;
	}

	public PreferencesManager getPreferencesManager() {
		return preferencesManager;
	}
	
	/*@Override
	public void render() {
		if (getScreen() instanceof LoadingScreen) {
			
			
			//Gdx.app.log("test", time/1000+"");
			//while (true) {
				if (assetManager.update()) {
					System.out.println("menu");
					//setScreen(new MenuScreen(this));
					if (getScreen()==null) {
						if (DEV_MODE) {
							setScreen(new MenuScreen(this));
						}else {
							setScreen(new SplashScreen(this));
						}
					}
					
			}else {
				System.out.println(assetManager.getProgress());
			}
			
			//Gdx.app.log("test", (System.currentTimeMillis()-time)/1000+"");
		}else {
			
		}
		super.render();
			if (DEV_MODE) {
				fpsLogger.log();
			}
		
	}*/
	
    @Override
    public void setScreen(Screen screen) {
    	super.setScreen(screen);
    	if (getScreen() instanceof LoadingScreen) {
    		loadAssets();
		}
    	
    }
	
	public AssetManager getAssetManager() {
		return assetManager;
	}
	
	@Override
	public void dispose() {
		super.dispose();
		databaseHelper.dispose();
		musicManager.dispose();
		
		soundManager.dispose();
		assetManager.dispose();
	}

	public void loadAssets(){
		assetManager.load("image-atlas/image-atlas.atlas", TextureAtlas.class);
		assetManager.load("game-images/game-images.atlas", TextureAtlas.class);
		assetManager.load("skins/uiskin.json",Skin.class);
		assetManager.load("effects/bubble.particle", ParticleEffect.class);
		musicManager.loadAssets();
		soundManager.loadAssets();
	}

	public SqliteDatabaseHelper getDatabaseHelper() {
		return databaseHelper;
	}

	public void setDatabaseHelper(SqliteDatabaseHelper databaseHelper) {
		this.databaseHelper = databaseHelper;
	}

	public OrthographicCamera getCamera() {
		return camera;
	}

	public GameManager getGameManager() {
		return gameManager;
	}


}
